package com.atguigu.mybatis021.controller;


import com.atguigu.mybatis021.pojo.Employee;
import com.atguigu.mybatis021.service.EmpService;
import com.atguigu.mybatis021.utils.R;
import com.atguigu.mybatis021.vo.req.AndEmpVo;
import com.atguigu.mybatis021.vo.req.UpdateEmpVo;
import com.atguigu.mybatis021.vo.resp.EmpRespVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "员工列表")
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping("/emp/{id}")
    @Operation(summary = "查询员工")
    @Parameters(
            @Parameter(name ="id",description ="员工id",in = ParameterIn.PATH,required =true)
    )
    public R<EmpRespVo> getEmpId(@PathVariable Integer id) {

        Employee employee = empService.getEmpId(id);
        EmpRespVo empRespVo = new EmpRespVo();
        BeanUtils.copyProperties(employee,empRespVo);
        return R.ok(empRespVo);
    }

    @PostMapping("/emp")
    @Operation(summary = "添加员工")
    @Parameters(
            @Parameter(name ="employee",description ="员工信息",in = ParameterIn.DEFAULT,required =true)
    )
    public R addEmp(@RequestBody AndEmpVo andEmpVo) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(andEmpVo,employee);
        empService.addEmp(employee);
        return R.ok(200,"添加成功");
    }

    @PutMapping("/emp")
    @Operation(summary = "更新员工")
    @Parameters(
            @Parameter(name ="employee",description ="员工信息",in = ParameterIn.DEFAULT,required =true)
    )
    public R updateEmpId(@RequestBody UpdateEmpVo updateEmpVo) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(updateEmpVo,employee);
        empService.updateEmpId(employee);
        return R.ok(200,"更新成功");
    }

    @DeleteMapping("/emp/{id}")
    @Operation(summary = "删除员工")
    @Parameters(
            @Parameter(name ="id",description ="员工id",in = ParameterIn.PATH,required =true)
    )
    public R deleteEmpId(@PathVariable Integer id) {
        empService.deleteEmpId(id);
        return R.ok();
    }
    //查询所有员工信息
    @GetMapping("/emps")
    @Operation(summary = "查询所有员工")
    @Parameters(
            @Parameter(name ="employee",description ="员工信息",in = ParameterIn.DEFAULT,required =true)
    )
    public List<Employee> getEmps() {
        //添加分页功能
        PageHelper.startPage(1, 3);
        List<Employee> employee = empService.getEmps();
        return employee;
    }

//    //分页查询所有员工信息
//    @GetMapping("emps/page")
//    @Operation(summary = "分页查询所有员工")
//    @Parameters(
//            @Parameter(name ="employee",description ="员工信息",in = ParameterIn.DEFAULT,required =true)
//    )
//    public R getEmpsPage(
//            @Parameter(name = "pageNum",description = "页码",required = true)
//            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
//            @Parameter(name = "pageSize",description = "每页条数",required = true)
//            @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize){
//        if (pageNum == null || pageSize == null){
//            return R.error("页码或每页条数不能为空");
//        }else{
//
//            PageHelper.startPage(pageNum, pageSize);
//            List<Employee> empList = empService.getEmps();
//            PageInfo pageInfo = new PageInfo(empList);
//            return  R.ok(pageInfo);
//        }
//
//
//        }



    }








